package com.bulletelc.apollopre.module.main.center.setting.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.framework.BaseActivity;
import com.bulletelc.apollopre.utils.MyUtil;
import com.bulletelc.apollopre.widget.FoolBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by CX on 2018/1/15.
 */

public class ModifyPasswordActivity extends BaseActivity implements View.OnClickListener, FoolBar.ClickCallback {

    private Button btnPost;
    private EditText etNewPwd, etAgPwd;
    private FoolBar foolBar;

    @Override
    public int getLayoutRes() {
        return R.layout.modify_password_activity;
    }

    @Override
    public void assignViews() {
        btnPost = findViewById(R.id.btn_post);
        etNewPwd = findViewById(R.id.et_new_password);
        etAgPwd = findViewById(R.id.et_password_confirm);
        foolBar = findViewById(R.id.flBar);
    }

    @Override
    public void setListener() {
        btnPost.setOnClickListener(this);
        foolBar.setClickCallback(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        foolBar.setTitle(this.getResources().getString(R.string.setting_modify_password));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_post:
                postSettingPassword();
                break;
        }
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    /**
     * 提交用户修改
     */
    private void postSettingPassword() {

        String newPwd = etNewPwd.getText().toString().trim();
        String agPwd = etAgPwd.getText().toString().trim();
        String signatureData = getIntent().getStringExtra("signature_encrypt_data");
        String mobileNum = getIntent().getStringExtra("mobile_number");
        if (newPwd.length() < 6) {
            Toast.makeText(this, "密码不能少于6位", Toast.LENGTH_SHORT).show();
            return;
        }
        String json = null;
        String url;
        JSONObject obj = new JSONObject();
        // 从忘记密码跳过来
        if (signatureData!=null) {
            JSONObject objSub = new JSONObject();
            url = NetWorkPath.POST_FORGET_PASSWORD;
            try {
                objSub.put("password", newPwd);
                objSub.put("password_twice", agPwd);
                objSub.put("signature_encrypt_data", signatureData);
                objSub.put("mobile_number", mobileNum);
                obj.put("ForgetpasswordForm", objSub);
                json = obj.toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            JSONObject objSub = new JSONObject();
            url = NetWorkPath.POST_MODIFY_PASSWORD;
            try {
                objSub.put("password", newPwd);
                objSub.put("password_twice", agPwd);
                obj.put("SettingPasswordForm", objSub);
                json = obj.toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        if (json != null) {
            RequestBody body = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), obj.toString());
            OkGo.<String>post(url)
                    .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                    .upRequestBody(body)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            parseData(response);
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                        }
                    });
        } else {
            Toast.makeText(this, "请求参数错误", Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 解析返回数据结果
     * @param response
     */
    private void parseData(Response<String> response) {
        try {
            JSONObject obj = new JSONObject(response.body().toString());
            JSONObject objMeta = new JSONObject(obj.getString("meta"));
//            LogUtil.i(response.body().toString());
            if(GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code").toString())) {
                this.finish();
                Toast.makeText(this, objMeta.getString("msg"), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, objMeta.getString("msg"), Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
//            LogUtil.i(e.getMessage());
        }
    }

}
